<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    .shenren{display: none}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span>编辑通知公告</span><div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" key="0" lay-submit="" lay-filter="add" style="background: #aaa">存草稿</button>
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" key="1" lay-submit="" lay-filter="add">发布</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
    <div class="main-container">
        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="title" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>通知类型</label>
                        <div class="layui-input-block" id="tongtypeDiv"></div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>发布单位</label>
                        <div class="layui-input-block">
                            <input type="text" id="facom_name" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>接收范围</label>
                        <div class="layui-input-block" id="jietypeDiv"></div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item shenren" id="jietype_role">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>角色</label>
                        <div class="layui-input-block">
                            <div id="roleid"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item shenren" id="jietype_dept">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>部门</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=deptid&type=dept&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="deptid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=deptid&type=dept&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="deptid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item shenren" id="jietype_gang">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>岗位</label>
                        <div class="layui-input-block">
                            <div id="gangid"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item shenren" id="jietype_user">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>用户</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=userid&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="userid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=userid&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="userid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                </div>
            </div>






            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea"><text>*</text>内容</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    $("#commentForm").validate({
        rules: {
            title: {required: true},
            explain: {required: true},
        },
        messages:{
            title: {required: "请输入"},
            explain: {required: "请输入"},
        }
    });


    var jietype;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_notice_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                $("#facom_name").val(data.facom_name);

                $("#title").val(data.title);
                $("#explain").val(data.explain);



                var tongtype = res.data.tongtype;
                var tongtype_str = '';
                for (var key in tongtype){
                    if(key == data.tongtype){
                        tongtype_str += '<input type="radio" name="tongtype" value="'+key+'" checked title="'+tongtype[key]+'">';
                    }else{
                        tongtype_str += '<input type="radio" name="tongtype" value="'+key+'" title="'+tongtype[key]+'">';
                    }
                }
                $('#tongtypeDiv').html(tongtype_str);


                jietype = res.data.jietype;
                var jietype_str = '';
                for (var key in jietype){
                    if(key == data.jietype){
                        jietype_str += '<input type="radio" name="jietype" value="'+key+'" checked title="'+jietype[key]+'" lay-filter="jietype">';
                    }else{
                        jietype_str += '<input type="radio" name="jietype" value="'+key+'" title="'+jietype[key]+'" lay-filter="jietype">';
                    }
                }
                $('#jietypeDiv').html(jietype_str);



                var gangidXmObj = xmselect_duo('gangid', res.data.gangArr, '', function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#gangid").find('xm-select').removeClass('error');
                    }else{
                        $("#gangid").find('xm-select').addClass('error');
                    }
                });
                var roleidXmObj = xmselect_duo('roleid', res.data.roleArr, '', function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#roleid").find('xm-select').removeClass('error');
                    }else{
                        $("#roleid").find('xm-select').addClass('error');
                    }
                });



                $("#jietype_"+data.jietype).show();
                if(data.jietype == 'dept'){
                    if(data.deptid_info.length > 0){
                        tan_tags_init('#deptid', data.deptid_info);
                    }
                }
                if(data.jietype == 'gang'){
                    var gangidXmObj = xmselect_duo('gangid', res.data.gangArr, data.gangid, function (res) {
                        if(res.arr && res.arr.length > 0){
                            $("#gangid").find('xm-select').removeClass('error');
                        }else{
                            $("#gangid").find('xm-select').addClass('error');
                        }
                    });
                }
                if(data.jietype == 'role'){
                    var roleidXmObj = xmselect_duo('roleid', res.data.roleArr, data.roleid, function (res) {
                        if(res.arr && res.arr.length > 0){
                            $("#roleid").find('xm-select').removeClass('error');
                        }else{
                            $("#roleid").find('xm-select').addClass('error');
                        }
                    });
                }
                if(data.jietype == 'user'){
                    if(data.userid_info.length > 0){
                        tan_tags_init('#userid', data.userid_info);
                    }
                }





                public_upload_file_load('upload_files', res.data.files)
                render_from();
                initParseMoney();
            }
        });


        form.on('radio(jietype)',function (data) {
            for (var key in jietype){
                $("#jietype_"+key).hide();
            }
            $("#jietype_"+data.value).show();
        });

        form.on('submit(add)', function(data){
            data.field.status = $(this).attr('key');
            data.field.jieer = '';


            var validRes = $("#commentForm").valid();


            if(data.field.jietype == 'dept'){
                data.field.jieer = tan_tags_getvalue("#deptid", 'dept');
                if(!data.field.jieer){
                    $("#deptid").parents('.tanxian').addClass('tanerror');
                    $('#deptid_tanerror').text('请选择');
                    validRes = false;
                }else{
                    $("#deptid").parents('.tanxian').removeClass('tanerror');
                    $('#deptid_tanerror').text('');
                }
            }
            if(data.field.jietype == 'gang'){
                data.field.jieer = data.field.gangid;
                if(!data.field.jieer){
                    $("#gangid").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#gangid").find('xm-select').removeClass('error');
                }
            }
            if(data.field.jietype == 'role'){
                data.field.jieer = data.field.roleid;
                if(!data.field.jieer){
                    $("#roleid").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#roleid").find('xm-select').removeClass('error');
                }
            }
            if(data.field.jietype == 'user'){
                data.field.jieer = tan_tags_getvalue("#userid", 'user');
                if(!data.field.jieer){
                    $("#userid").parents('.tanxian').addClass('tanerror');
                    $('#userid_tanerror').text('请选择');
                    validRes = false;
                }else{
                    $("#userid").parents('.tanxian').removeClass('tanerror');
                    $('#userid_tanerror').text('');
                }
            }

            if(!validRes){return false;}

            if(data.field.explain == "<p><br></p>"){
                facade.error("请输入内容");return false;
            }

            delete data.field.roleid;
            delete data.field.gangid;

            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            if(data.field.status > 0){
                layer.confirm("<div class='tit'>是否确认发布？</div><div class='exp'>发布后，接受范围的用户将可查看该通知。</div>", {
                    title:'操作确认',
                    icon: 3,
                    area:['450px','220px'],
                    btn:['确定']
                }, function(conindex){
                    facade.ajax({url: ask_notice_edit, data:data.field}).done(function(res){
                        if (res.code === 2000) {
                            dataPostTimeOut();
                        }
                    });
                    layer.close(conindex);
                })
            }else{
                facade.ajax({url: ask_notice_edit, data:data.field}).done(function(res){
                    if (res.code === 2000) {
                        dataPostTimeOut();
                    }
                });
            }



            return false;
        });

    });



</script>
</body>
</html>
